﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ProjectProperties;
using BusinessLayer;


namespace GUI
{
    public partial class Company : Form
    {
        private DataSet dsCompany;
        private bool bNew;
        private int nCompRowCnt, nCompId;

        public Company()
        {
            InitializeComponent();
            nCompRowCnt = 0;
            nCompId = 0;
            bNew = false;
        }

        private void Company_Load(object sender, EventArgs e)
        {
            DisplayTop100Company();
            Enable();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            bNew = true;
            Disable();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CompanySQL oCompanySQL = new CompanySQL();
            CompanyInfo oCompanyInfo = new CompanyInfo();
            oCompanyInfo.Company_Id = nCompId;
            oCompanyInfo.Company_Name = txtCompany.Text;
            if(bNew == true )
                oCompanySQL.InsertCompany(oCompanyInfo);
            else
                oCompanySQL.UpdateCompany(oCompanyInfo);
            DisplayTop100Company();
            Enable(); 
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DisplayTop100Company();
            Enable();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSelectCompany_TextChanged(object sender, EventArgs e)
        {
            CompanySQL oCompanySQL = new CompanySQL();
            CompanyInfo oCompanyInfo = new CompanyInfo();
            dsCompany = new DataSet();
            oCompanyInfo.Company_Name = txtSelectCompany.Text + "%";
            dsCompany = oCompanySQL.SelectTop100Company(oCompanyInfo);
            dtgSelectCompany.DataSource = dsCompany;
            nCompRowCnt = dsCompany.Tables[0].Rows.Count;
            if (nCompRowCnt != 0)
            {
                dtgSelectCompany.NavigateTo(0, "Table");
                dtgSelectCompany.Select(0);
            }
        }

        private void txtSelectCompany_KeyDown(object sender, KeyEventArgs e)
        {
            int nCurRow = dtgSelectCompany.CurrentCell.RowNumber;
            if (e.KeyCode == Keys.Up)
            {
                if (nCurRow > 0)
                {
                    nCurRow = nCurRow - 1;
                    dtgSelectCompany.CurrentCell = new DataGridCell(nCurRow, 0);
                    if ((nCurRow + 1) != nCompRowCnt)
                    {
                        dtgSelectCompany.UnSelect(nCurRow + 1);
                    }
                    dtgSelectCompany.Select(nCurRow);
                }
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (nCurRow < nCompRowCnt - 1)
                {
                    dtgSelectCompany.UnSelect(nCurRow);
                    nCurRow = nCurRow + 1;
                    dtgSelectCompany.CurrentCell = new DataGridCell(nCurRow, 0);
                    dtgSelectCompany.Select(nCurRow);
                }
            }
            else if (e.KeyCode == Keys.Enter)
            {
                if (nCompRowCnt != 0)
                {
                    bNew = false;
                    Disable();
                    txtCompany.Text = dtgSelectCompany[dtgSelectCompany.CurrentCell.RowNumber, 1].ToString();
                    nCompId = Convert.ToInt32(dtgSelectCompany[dtgSelectCompany.CurrentCell.RowNumber, 0]);
                }                
            }
        }

        #region Functions
        private void DisplayTop100Company()
        {
            CompanySQL oCompanySQL = new CompanySQL();
            dsCompany = new DataSet();
            dsCompany = oCompanySQL.DisplayTop100Company();
            dtgSelectCompany.DataSource = dsCompany;
            nCompRowCnt = dsCompany.Tables[0].Rows.Count;
            DataGridTableStyle dtgTs = new DataGridTableStyle();
            dtgTs.MappingName = "Table";
            dtgSelectCompany.TableStyles.Clear();
            dtgSelectCompany.TableStyles.Add(dtgTs);
            if (nCompRowCnt != 0)
            {
                dtgSelectCompany.NavigateTo(0, "Table");
                dtgSelectCompany.Select(0);                
                dtgSelectCompany.TableStyles["Table"].SelectionBackColor = Color.Gainsboro;
                dtgSelectCompany.TableStyles["Table"].SelectionForeColor = Color.Red;
                dtgSelectCompany.TableStyles["Table"].HeaderBackColor = Color.WhiteSmoke;
                dtgSelectCompany.TableStyles["Table"].GridColumnStyles[0].Width = 0;
                dtgSelectCompany.TableStyles["Table"].GridColumnStyles[1].Width = 220;
            }            
        }

        private void Enable()
        {
            txtCompany.Clear();
            txtSelectCompany.Enabled = true;
            dtgSelectCompany.Visible = true;
            btnNew.Enabled = true;
            btnSave.Enabled = false;
            btnCancel.Enabled = false;                        
            txtCompany.Enabled = false;
            txtSelectCompany.Focus();
        }

        private void Disable()
        {
            txtSelectCompany.Clear();
            txtSelectCompany.Enabled = false;
            dtgSelectCompany.Visible = false;
            btnNew.Enabled = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            txtCompany.Enabled = true;
            txtSelectCompany.BackColor = Color.White;
            txtCompany.Focus();
        }
        #endregion
    }
}
